#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int N;

void Insertion_Sort(int arr[], int brr[]) {
    int flag = 0;
    if(N>=10){
        for (int i = 1; i <= N; i++) {
            sort(arr, arr + i);
            if (flag) {
                cout << "Insertion Sort" << endl << arr[0];
                for (int j = 1; j < N; ++j)
                    cout << " " << arr[j];
                exit(0);
            }
            if (equal(arr, arr + N, brr))
                flag = 1;
        }
    }else{
        for (int i = 1; i <= N; i++) {
            sort(arr, arr + i+1);
            if (flag) {
                cout << "Insertion Sort" << endl << arr[0];
                for (int j = 1; j < N; ++j)
                    cout << " " << arr[j];
                exit(0);
            }
            if (equal(arr, arr + N, brr))
                flag = 1;
        }
    }
}

void Heap_Sort(int arr[], int brr[]) {
    int flag = 0;
    for (int i = N; i > 0; --i) {
        make_heap(arr, arr + i);
        if (flag) {
            cout << "Heap Sort" << endl << arr[0];
            for (int i = 1; i < N; ++i)
                cout << " " << arr[i];
            exit(0);
        }
        if (equal(arr, arr + N, brr))
            flag = 1;
        swap(arr[0], arr[i - 1]);
    }
}

int main() {
    cin >> N;
    int arr1[100], arr2[100], brr[100];
    for (int i = 0; i < N; ++i) {
        cin >> arr1[i];
        arr2[i] = arr1[i];
    }
    for (int i = 0; i < N; ++i)
        cin >> brr[i];
    Insertion_Sort(arr1, brr);
    Heap_Sort(arr2, brr);
    return 0;
}

